package cn.explink.dop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.explink.dop.dao.OrderDao;
import cn.explink.dop.dao.ReturnDao;
import cn.explink.dop.dto.ReturnSettleDTO;
import cn.explink.dop.entity.vo.ReturnRecordVO;
import cn.explink.dop.entity.vo.ReturnVO;
import cn.explink.dop.entity.vo.SettleAccoutsRecordVO;
import cn.explink.dop.service.ReturnService;
import cn.explink.eap.core.common.service.impl.CommonServiceImpl;

@Service("returnService")
public class ReturnServiceImpl extends CommonServiceImpl implements ReturnService {

	@Autowired
	private ReturnDao returnDao;
	@Autowired
	private OrderDao orderDao;

	/*
	 * 归班结算下查询按钮的查询归班单方法
	 */
	@Override
	public Map<String, Object> queryReturnForSettle(ReturnVO returnVO, int pageNum, int pageSize) {
		Map<String, Object> map = this.returnDao.queryReturnForSettle(returnVO, pageNum, pageSize);
		return map;
	}

	/*
	 * jdbc方式的返回主键插入更新
	 * 
	 * @Override public Map<String, Object> creatReturn(OrderVO orderVO, int
	 * pageNum, int pageSize) { //这个方法里调用的方法的返回类型需要再仔细看一下，要等ReturnVO持久化之后再作调整 //
	 * List<OrderVO> orderVOList = this.orderDao.finds(orderVO); // 1
	 * .按照归班单查出所需订单表字段内容 总单数、总金额、时间、送货人 List<OrderVO> list =
	 * this.orderDao.queryForReturn(orderVO);
	 * 
	 * // 2 .将拼装好的归班单VO的内容插入到归班单表中并返回主键 ReturnVO returnVO= new ReturnVO(); float
	 * money=0f; for(OrderVO vo : list){
	 * money+=vo.getTotalPrice().doubleValue(); } //
	 * returnVO.setSendPersonName(list.get(0).getDeliveryManName());
	 * returnVO.setSendPersonName("sss"); returnVO.setMoney(money);
	 * returnVO.setNumber(list.size()); returnVO.setCreateTime(new Date(0)); //
	 * returnVO.setBillNo(Tools.getRandomSeq(null)); returnVO.setStatus("未上交");
	 * returnVO.setOrderVOList(list); returnVOList.add(returnVO); String key =
	 * returnDao.creatReturn(returnVOList);
	 * 
	 * // 3 .根据插入到归班表的数据返回的主键更新订单表，表示所更新的订单属于哪个归班单
	 * returnDao.updateOrderByReturnPrimaryKey(key);
	 * 
	 * // 4 .查询归班单返回结果到前台 Map<String, Object> map =
	 * returnDao.queryReturn(returnVO, pageNum, pageSize); return map; }
	 */

	@Override
	public int updateReturnStatus(String billno) {
		int count = this.returnDao.updateReturnStatus(billno);
		return count;
	}

	@Override
	public void deleteReturn(String billno) {
		this.returnDao.deleteReturn(billno);
	}

	@Override
	public int deleteOrder(String returnid) {
		int count = this.returnDao.deleteOrder(returnid);
		return count;
	}

	/*
	 * 派费结算页面的service查询方法
	 */
	@Override
	public Map<String, Object> queryReturn(ReturnVO returnVO, int pageNum, int pageSize) {

		Map<String, Object> mapReturn = this.returnDao.queryReturn11(returnVO, pageNum, pageSize);
		return mapReturn;
	}

	@Override
	public Map<String, Object> queryReturnByBillNo(String billno) {
		Map<String, Object> map = this.returnDao.queryReturnByBillNo(billno);
		return map;
	}

	/**
	 *
	 * @Title: getReturnSettle
	 * @Description: 分页获取归班反馈信息
	 * @author 孟凡岭
	 * @param @param returnSettleDTO
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @return
	 * @throws
	 */
	@Override
	public Map<String, Object> getReturnSettle(ReturnSettleDTO returnSettleDTO, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return this.returnDao.getReturnSettle(returnSettleDTO, pageNum, pageSize);
	}

	/**
	 *
	 * @Title: upMoney
	 * @Description: 配送员交款
	 * @author 孟凡岭
	 * @param @param deliveryIds
	 * @param @param returnDate
	 * @throws
	 */
	@Override
	public void upMoney(List<String> deliveryIds, List<String> returnDates) throws Exception {
		/**
		 * 在交款之前再查询一次配送员id和结算信息，防止多页面操作数据发生误操作
		 */
		List<ReturnRecordVO> recordList = new ArrayList<ReturnRecordVO>();
		for (int i = 0; i < deliveryIds.size(); i++) {
			/**
			 * 更新订单
			 */
			List<ReturnRecordVO> list = this.returnDao.upMoney(deliveryIds.get(i), returnDates.get(i));
			recordList.addAll(list);
		}
		this.returnDao.batchSave(recordList);
		this.returnDao.updateOrderReturn(recordList, returnDates);
	}

	/**
	 *
	 * @Title: settleMoney
	 * @Description: 结算派费
	 * @author 孟凡岭
	 * @param @param deliveryIds
	 * @param @param completeTimes
	 * @throws
	 */
	@Override
	public void settleMoney(List<String> deliveryIds, List<String> completeTimes) throws Exception {
		// TODO Auto-generated method stub
		/**
		 * 在结算之前再查询一次配送员id和结算信息，防止多页面操作数据发生误操作
		 */
		List<SettleAccoutsRecordVO> recordList = new ArrayList<SettleAccoutsRecordVO>();
		for (int i = 0; i < deliveryIds.size(); i++) {
			/**
			 * 更新订单
			 */
			List<SettleAccoutsRecordVO> list = this.returnDao.settleMoney(deliveryIds.get(i), completeTimes.get(i));
			recordList.addAll(list);
		}
		this.returnDao.batchSave(recordList);
		this.returnDao.updateSettleRecord(recordList, completeTimes);
	}
}
